<template>
  <div class="mod--station">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="shopName"
            :label="$t('coupon.shopName') + ':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.shopName"
              clearable
              type="text"
              :placeholder="$t('coupon.shopName')"
            />
          </el-form-item>
          <el-form-item
            prop="stationName"
            :label="$t('order.pickUpPoint') + ':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.stationName"
              clearable
              type="text"
              :placeholder="$t('order.pickUpPoint')"
            />
          </el-form-item>
          <el-form-item
            prop="addr"
            :label="$t('shop.address') + ':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.addr"
              clearable
              type="text"
              :placeholder="$t('shop.address')"
            />
          </el-form-item>
          <el-form-item
            prop="account"
            :label="$t('shop.accountNumber') + ':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.account"
              clearable
              type="text"
              :placeholder="$t('shop.accountNumber')"
            />
          </el-form-item>
          <el-form-item
            prop="status"
            :label="$t('brand.status') + ':'"
            class="search-form-item"
          >
            <el-select
              v-model="searchForm.status"
              clearable
              :placeholder="$t('brand.status')"
            >
              <el-option
                :label="$t('seckill.close')"
                :value="0"
              />
              <el-option
                :label="$t('shop.business')"
                :value="1"
              />
              <el-option
                :label="$t('components.platformOffline')"
                :value="2"
              />
              <el-option
                :label="$t('shop.underReview')"
                :value="3"
              />
              <el-option
                :label="$t('productComm.noPass')"
                :value="4"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('product.search') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch()"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con station-table">
        <el-table
          ref="stationListTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <el-table-column
            fixed
            type="index"
            :label="$t('brand.serialNumber')"
            width="100"
          />

          <el-table-column
            fixed
            prop="shopName"
            :label="$t('coupon.shopName')"
            width="260"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.shopName || '-' }}</span>
            </template>
          </el-table-column>

          <el-table-column
            prop="stationName"
            :label="$t('order.pickUpPoint')"
            width="260"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.stationName || '-' }}</span>
            </template>
          </el-table-column>

          <el-table-column
            prop="addr"
            :label="$t('shop.address')"
            width="260"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.province }}{{ scope.row.city }}{{ scope.row.area }}{{ scope.row.addr }}</span>
            </template>
          </el-table-column>

          <el-table-column
            prop="phone"
            :label="$t('platform.phoNumber')"
            width="180"
          >
            <template #default="scope">
              <span
                v-if="scope.row.phonePrefix"
              >{{ scope.row.phonePrefix }}-</span>
              <span>{{ scope.row.phone }}</span>
            </template>
          </el-table-column>

          <el-table-column
            prop="account"
            :label="$t('shop.accountNumber')"
            width="auto"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.account || '-' }}</span>
            </template>
          </el-table-column>

          <el-table-column
            prop="status"
            :label="$t('brand.status')"
            width="auto"
          >
            <template #default="scope">
              <span
                v-if="scope.row.status === 4"
                class="tag-text"
              >{{ $t('components.auditNotPassed') }}</span>
              <span
                v-else-if="scope.row.status === 3"
                class="tag-text"
              >{{ $t('shop.underReview') }}</span>
              <span
                v-else-if="scope.row.status === 2"
                class="tag-text"
              >{{ $t('components.platformOffline') }}</span>
              <span
                v-else-if="scope.row.status === 1"
                class="tag-text"
              >{{ $t('shop.business') }}</span>
              <span
                v-else
                class="tag-text"
              >{{ $t('seckill.close') }}</span>
            </template>
          </el-table-column>

          <el-table-column
            fixed="right"
            align="center"
            :label="$t('publics.operating')"
            width="230"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="isAuth('platform:station:update')"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row.stationId)"
                >
                  {{ $t('coupon.viewDetails') }}
                </div>
                <div
                  v-if="isAuth('platform:station:update') && scope.row.status<2"
                  class="default-btn text-btn"
                  @click="offlineSeckillHandle(scope.row)"
                >
                  {{ $t('product.violation') }}
                </div>
                <div
                  v-if="isAuth('platform:station:update') && scope.row.status > 1"
                  class="default-btn text-btn"
                  @click="stationAuditHandle(scope.row.stationId)"
                >
                  {{ scope.row.status === 2?$t('prodList.offShelfManage'):scope.row.status === 3?$t('coupon.checkPending'):$t('productComm.noPass') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />
    <!-- 商品审核弹窗 -->
    <offline-audit
      v-if="stationAuditVisible"
      ref="offlineAuditRef"
      select-url="/platform/station/getOfflineHandleEventByStationId"
      update-url="/platform/station/auditStation"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import AddOrUpdate from './add-or-update.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { isAuth } from '@/utils/index.js'

const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = reactive({
  shopName: '',
  stationName: '',
  addr: '',
  account: '',
  status: ''
}) // 搜索

onMounted(() => {
  getDataList()
})

let tempSearchForm = null // 保存上次点击查询的请求条件
const dataList = ref([])
const getDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm))
  }
  http({
    url: http.adornUrl('/platform/station/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.pageSize : pageParam.pageSize
        },
        tempSearchForm
      )
    )
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total

    // 末尾页数据为空重新请求
    if (!dataList.value.length && data.current > 1) {
      page.currentPage = 1
      getDataList()
    }
  })
}

const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref(null)
// 新增 / 修改
const onAddOrUpdate = (id) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(id)
  })
}

const offlineSeckillHandle = (row) => {
  // '确定对活动[' + row.stationName + ']进行下线操作？如是，请填写下线理由'
  let tips = $t('seckill.stationOfflineTip')
  tips = tips.replace('STATIONNAME', row.stationName)
  ElMessageBox.prompt(tips, $t('text.tips'), {
    confirmButtonText: $t('coupon.confirm'),
    cancelButtonText: $t('coupon.cancel'),
    inputPattern: /\s\S+|S+\s|\S/, // 不能全为空格 空字符串
    inputErrorMessage: $t('seckill.offlineReasonTip'),
    inputValidator: (value) => {
      if (value.length > 100) {
        return $t('seckill.offlineReasonTip1')
      }
    }
  }).then((obj) => {
    http({
      url: http.adornUrl('/platform/station/offline'),
      method: 'post',
      data: http.adornData({
        handleId: row.stationId,
        offlineReason: obj.value || null
      })
    }).then(() => {
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          getDataList(page)
        }
      })
    })
  })
}

const stationAuditVisible = ref(false)
const offlineAuditRef = ref(null)
// 弹窗处理
const stationAuditHandle = (id) => {
  stationAuditVisible.value = true
  nextTick(() => {
    offlineAuditRef.value?.init(id)
  })
}

/**
 * 刷新回调
 */
const refreshChange = () => {
  getDataList(page)
}

const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

const searchFormRef = ref(null)
/**
 * 重置表单
 */
const onResetSearch = () => {
  searchFormRef.value.resetFields()
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList()
}

const onPageChange = (val) => {
  page.currentPage = val
  getDataList()
}

</script>
